<?php
    header("content-type:text/html;charset=utf-8");
    //连接数据库
    $link = mysqli_connect("localhost","root","20201002983","test");
	if (!$link) {
		die("连接失败: " . mysqli_connect_error());
	}
    $username = $_POST['name'];
	$password = $_POST['password'];
    //查看表user用户名是否存在或为空
    $sql_select = "SELECT * FROM users WHERE name = '$username'";
    //result必需规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。
    $select = mysqli_query($link,$sql_select);
    $num = mysqli_num_rows($select);//函数返回结果集中行的数量
    if($username == "" || $password == "")
    {
        $url = '../register.html';
        echo '<script>alert("请确认信息的完整性");</script>';
        echo "<script>location.href='$url'</script>";//已存在账户名输出错误
    }else if(!is_numeric($password)){
        // echo "数组值的数量是".sizeof($password)." ";
        $url = '../register.html';
        echo '<script>alert("密码不是纯数字！");</script>';
        echo "<script>location.href='$url'</script>";//已存在密码输入错误
        // echo '输入的是:'+$password;
    }else if($num){
        $url = '../register.html';
        echo '<script>alert("此id已有黄花菜使用");</script>';
        echo "<script>location.href='$url'</script>";//已存在账户名输出错误
    }else{
        $sql="insert into users(name,password) values('$username','$password')";
        $result=mysqli_query($link,$sql);
        //判断是否注册后显示内容
        if(!$result)
        {

            $url = '../register.html';
            echo "<script>alert('创建世界失败');</script>";
            echo "<script>location.href='$url'</script>";
        }
        else
        {
            $url = '../login.html';
            echo '<script>alert("成功成为一棵新白菜！现在去连接世界吧!");</script>';
            echo "<script>location.href='$url'</script>";
        }
    }


    // if(empty($_POST["name"])) {
    //     die("输入值不能为空");
    // }
    // if(strlen($_POST["password"])>6){
    //     die("密码是六位数字");
    // }

    // require __DIR__ . "/database.php";
    // mysqli_set_charset($link,"utf8");
    // $sql = "insert into users (name,password) values (?,?)";
    // $stmt = $mysqli ->stmt_init();
    // if( ! $stmt ->prepare ($sql) ){
    //     die("SQL error: " . $mysqli ->error);
    // };
    // $stmt ->bind_param($_POST["name"],$_POST["password"]);
    // if($stmt ->execute()) {
    // } else {
    //     die($mysqli ->error . " " . $mysqli -> errno);
    // }
    // print_r($_POST);
    ?>